package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: YddAdvimgController
 * Description:  广告图
 * date: 2022/2/25 20:55
 *
 * @author zjf95
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
@SuppressWarnings("all")
public class YddAdvimgController {


    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddAdvposService yddAdvposService;

    //=&=&page=1&limit=10
    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, int page, int limit) {

        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);
        Page<YddAdDto> yddAdDtoPage = new Page<>();

        LambdaQueryWrapper<YddAdvimg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(advimgpos != null, YddAdvimg::getAdvimgpos, advimgpos);

        lambdaQueryWrapper.like(advimgdesc != null, YddAdvimg::getAdvimgdesc, advimgdesc);

        yddAdvimgService.page(yddAdvimgPage, lambdaQueryWrapper);

        BeanUtils.copyProperties(yddAdvimgPage, yddAdDtoPage, "records");

        List<YddAdvimg> records = yddAdvimgPage.getRecords();

        List<YddAdDto> list = records.stream().map((item) -> {
            YddAdDto yddAdDto = new YddAdDto();
            BeanUtils.copyProperties(item, yddAdDto);

            log.info(item.toString());

            String itemAdvimgpos = item.getAdvimgpos();

            int yddAdvposId = Integer.parseInt(itemAdvimgpos);

            YddAdvpos yddAdvpos = yddAdvposService.getById(yddAdvposId);

            yddAdDto.setAdvposname(yddAdvpos.getAdvposdesc());

            return yddAdDto;

        }).collect(Collectors.toList());

        yddAdDtoPage.setRecords(list);

        PageResult pageResult = PageResult.init(yddAdDtoPage.getTotal(), yddAdDtoPage.getSize(), yddAdDtoPage.getCurrent(), yddAdDtoPage.getRecords());

        return ResultData.ok("", pageResult);
    }
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam Long id){

        log.info("id为:",id);

        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);

        return ResultData.ok("",yddAdvimg);

    }
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg){

        log.info("修改后的对象为:{}",yddAdvimg);

        yddAdvimgService.updateById(yddAdvimg);

        return ResultData.ok("","修改成功");
    }
    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvimg yddAdvimg){

        log.info("要添加的对象为:{}", yddAdvimg);

        yddAdvimgService.save(yddAdvimg);

        return ResultData.ok("","添加成功");
    }
}
